package com.wangsd.web.sys.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.sys.entity.SysMenu;
import com.wangsd.web.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜单管理")
@RestController
@RequestMapping("/sys/sysMenu")
@Slf4j
public class SysMenuController extends MyController {
    @Resource
    private SysMenuService service;

    @ApiOperation(value = "分页查询列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             SysMenu queryObj) {
        log.debug("分页查询列表-controller");
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<SysMenu> list = service.selectAll();
        return Result.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "查询上级菜单")
    @GetMapping("/selectParentMenu")
    public Object selectParentMenu() {
        log.debug("查询上级菜单-controller");
        List<SysMenu> list = service.selectParentMenu();
        return Result.success(list);
    }

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/selectAll")
    public Object selectAll() {
        log.debug("查询所有菜单-controller");
        List<SysMenu> list = service.selectAll();
        return Result.success(list);
    }

    @ApiOperation(value = "删除菜单")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "菜单id", required = true) @PathVariable Long id) {
        log.debug("删除菜单-controller");
        service.delete(id);
        return Result.success();
    }


    @ApiOperation(value = "新增或者修改")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody SysMenu entity) {
        log.debug("新增或者修改-controller");
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "查询该角色拥有哪些菜单和按钮")
    @GetMapping("/{roleId}/selectByRoleId")
    public Result selectByRoleId(@ApiParam(value = "角色id", required = true) @PathVariable Long roleId) {
        log.debug("查询该角色拥有哪些菜单和按钮-controller");
        List<SysMenu> list = service.selectByRoleId(roleId);
        return Result.success(list);
    }
}
